<?php

namespace Home\Controller;

class CartController extends CommonController {

    public function confirm() {
//    echo session('returnUrl');exit;
        if (I("post.not_cart_submit") == 1) {
            session("not_cart_submit", 1);
        } else {
            session("not_cart_submit", 0);
        }

        $id = I("post.id") ? I("post.id") : session("cart_id");
        $num = I("post.num") ? I("post.num") : session("cart_num");
//   print_r(I("post.id"));
        if ($id) {
            session("cart_id", $id);
        } else {
            $this->error("购物车为空！");
            exit;
        }
        if ($num) {
            session("cart_num", $num);
        }
        if (session("userid") == '') {
            $this->redirect("Login/index");
        }
        $carts = array();
        foreach ($id as $k => $v) {
            if ($v > 0 && $num[$k] > 0) {
                $carts[$k]['id'] = $v;
                $carts[$k]['num'] = $num[$k];
            }
        }

        foreach ($carts as $k => $v) {
            $info = M("goods")->field("name,price,price_old,sid")->where("id = " . $v['id'] . "")->find();
            $sids[] = $info['sid'];
            $carts[$k]['name'] = $info['name'];
            $carts[$k]['price'] = $info['price'];
            $carts[$k]['price_old'] = $info['price_old'];
            $carts[$k]['sid'] = $info['sid'];
            $carts[$k]['num'] = $v['num'];
        }

        if ($sids) {
            $sids = array_unique($sids);
            $cartsArr = array();
            $total_price = 0;
            foreach ($sids as $k => $v) {
                $shop = M("shop")->field("title")->where("id = " . $v . "")->find();
                $cartsArr[$k]['id'] = $v;
                $cartsArr[$k]['title'] = $shop['title'];
                $shop_price = 0;
                $i = 0;
                foreach ($carts as $k2 => $v2) {
                    if ($v == $v2['sid']) {
                        $cartsArr[$k]['sub'][] = $v2;
                        $shop_price += $v2['price'] * $v2['num'];
                        $cartsArr[$k]['sub'][$i]['price_cha'] = sprintf("%.1f", $v2['price_old'] - $v2['price']);
                        $cartsArr[$k]['sub'][$i]['price_total'] = sprintf("%.1f", $v2['price'] * $v2['num']);
                        $i++;
                    }
                }
                $cartsArr[$k]['shop_price'] = sprintf("%.1f", $shop_price);
                $total_price += $cartsArr[$k]['shop_price'];
                $coupon = M("coupon")->where("sid = " . $v . " AND use_uid = " . session("userid") . " AND use_time = 0")->select();
                $cartsArr[$k]['coupon'] = $coupon;
            }
        }
        $addrs = M("addr")->where("uid = " . session("userid") . "")->order("id DESC")->select();
//       print_r($cartsArr);
        $this->assign('total_price', sprintf("%.1f", $total_price));
        $this->assign('cartsArr', $cartsArr);
        $this->assign('cartsNum', count($cartsArr));
        $this->assign('addrs', $addrs);
        $this->assign('num', $num);
        $this->assign('id', $id);

        $this->display();
    }

    public function addToCart() {//添加到购物车
        $id = I("post.id", 0, 'int');
        $num = I("post.num", 1, 'int');

        $carts = json_decode(cookie("cart"), true);

        
        $is_has = 0;
        if (empty($carts)) {
            $carts = array(0 => array("id" => $id, "num" => $num));
        } else {
            foreach ($carts as $k => $v) {
                if ($v['id'] == $id) {
                    $is_has++;
                    $carts[$k]['num'] = $v['num'] + $num;
                    break;
                }
            }
            if ($is_has == 0) {
                array_push($carts, array("id" => $id, "num" => $num));
            }
        }
        cookie("cart", json_encode($carts));
        if ($is_has == 0) {
            $goods = M("goods")->field("sid")->where("id = " . $id . "")->find();
            $shop = M("shop")->field("title")->where("id = " . $goods['sid'] . "")->find();
        }

        echo json_encode(array("sid" => $goods['sid'], "id" => $id, "title" => $shop['title']));
    }

    public function addCut() {//加减
        $id = I("post.id", 0, 'int');
        $num = I("post.num", 0, 'int');
      
        $carts = json_decode(cookie("cart"), true);
        if ($carts) {
            foreach ($carts as $k => $v) {
                if ($num == 0) {
                    if ($v['id'] == $id) {
                        unset($carts[$k]);
                    }
                } else {
                    if ($v['id'] == $id) {
                        $carts[$k]['num'] = $num;
                        break;
                    }
                }
            }
        }
        if ($carts) {
            cookie("cart", json_encode($carts));
        } else {
            cookie("cart", null);
        }
    }

    public function emptyCart() {
        cookie("cart", null);
    }

    public function saveAddr() {
        $arr = I("post.");
        $id = $arr['id'];
        $data['truename'] = $arr['truename'];
        $data['province'] = $arr['province'];
        $data['city'] = $arr['city'];
        $data['country'] = $arr['country'];
        $data['code'] = $arr['code'];
        $data['addr'] = $arr['addr'];
        $data['phone2'] = $arr['addrCode'] . "-" . $arr['addrNum'];
        if ($arr['addrExt']) {
            $data['phone2'] .= "-" . $arr['addrExt'];
        }
        $data['phone'] = $arr['addrMobi'];
        if ($id > 0) {
            M("addr")->where("id = " . $id . " AND uid = " . session("userid") . "")->save($data);
        } else {
            $data['uid'] = session("userid");
            $lastid = M("addr")->add($data);
            // echo M("addr")->getlastsql();
            echo $lastid;
        }
    }
    

    public function pay() {
        $id = I("get.id");
        $three = getTableFile('three');
        $alipay = json_decode($three[0]['alipay'],true);

        require_once("alipay/alipay.config.php");
        require_once("alipay/lib/alipay_submit.class.php");
        $order = M("order")->field("ordernum,order_money2")->where("id  = " . $id . "")->find();
//        print_r($order['ordernum']);exit;
        $goods = M("order_goods")->where("pid  = " . $id . "")->select();
        $pay_title = '';
        foreach ($goods as $k => $v) {
            $pay_title .= $v['gname'] . ",";
        }
        $order_money = $order['order_money2'];
        $pay_title = substr($pay_title, 0, -1); //订单名称
        $notify_url = "" . C('SITE_URL') . "/index.php/Notify/pay_single";
        $return_url = "" . C('SITE_URL') . "/index.php/Member/order";
        $order_ids = $order['ordernum'];
        include_once('alipay/payconfig.php');
        exit;
    }

}

?>
